[root@server1 ~]# fdisk /dev/vda
Managing Logical Volumes
Extending Logical Volumes
LVM comes with set of command line tools for implementing and managing LVM storage
Tools can be used in scripts, making them suitable for automation
Examples in this module use device vda and vdb and its partitions to illustrate LVM commands. In practice, these examples would use disk and disk partition devices used by system. |
Prepare physical device:
Use fdisk, gdisk, or parted to create new partition for use with LVM
Set the partition type:
Use Linux LVM on LVM partitions
Use 0x8e for MBR-style partitions
If necessary, use partprobe to register new partition with kernel
[root@server1 ~]# fdisk /dev/vda
Use m for help, p to print partition table, n to create new partition, t to change partition type, w to write changes, and q to quit. |
Create physical volume (PV):
Use pvcreate to label partition (or other physical device) for use with LVM as physical volume
Writes header directly to PV to store LVM configuration data
PV divided into physical extents (PE) of fixed size; for example, 4MiB blocks
To label multiple devices at same time, add space-delimited device names:
[root@server1 ~]# pvcreate /dev/vda2 /dev/vdb1
Create volume group (VG):
Use vgcreate to create volume group
VG size determined by total number of physical extents in pool
VG hosts logical volumes by allocating free PEs to LV
LV must have enough free PEs when LV is created
To define VG name and list one or more PVs to allocate to VG, add arguments
[root@server1 ~]# vgcreate vg-alpha /dev/vda2 /dev/vdb1
Create logical volume:
Use lvcreate with following options to create new logical volume from available physical extents in volume group:
Use -n to set LV name
Use -L to set LV size and identify VG that LV is created in
[root@server1 ~]# lvcreate -n hercules -L 2G vg-alpha
There are multiple ways to specify size
Use -L to specify size in bytes, mebibytes (binary megabytes, 1048576 bytes), or gibibytes (binary gigabytes)
Use -l to specify size as number of PEs
Examples:
lvcreate -L 128M: Size logical volume to exactly 128MiB
lvcreate -l 128: Size logical volume to exactly 128 extents
Total number of bytes depends on size of PE block on underlying PV
Tools may display logical volume name using:
Traditional name /dev/vgname/lvname
Kernel device mapper name /dev/mapper/vgname-lvname
Add file system:
Use mkfs to create XFS file system on new logical volume:
[root@server1 ~]# mkfs -t xfs /dev/vg-alpha/hercules
To make file system available across reboots, use mkdir to create mount point directory:
[root@server1 ~]# mkdir /mnt/hercules
Add entry to /etc/fstab:
/dev/vg-alpha/hercules /mnt/hercules xfs defaults 1 2
To mount all file systems in /etc/fstab, including the entry just added, run mount -a:
[root@server1 ~]# mount -a
Prepare file system:
Move all data that must be kept to another file system
Use umount to unmount file system
Remember to remove /etc/fstab entries associated with the file system
[root@server1 ~]# umount /mnt/hercules
| Removing a logical volume destroys any data stored on the logical volume. Back up or move your data before you remove the logical volume. |
Remove logical volume:
Use lvremove with device name as argument
[root@server1 ~]# lvremove /dev/vg-alpha/hercules
LV file system must be unmounted before running command
lvremove asks for confirmation
LV’s physical extents are now available for assignment to other LVs in volume group
Remove volume group:
Use vgremove with VG name as argument
[root@server1 ~]# vgremove vg-alpha
VG’s physical volumes are now available for assignment to VGs.
Remove physical volumes:
Use pvremove to remove physical volumes
Add space-delimited list of PV devices to remove more than one device
[root@server1 ~]# pvremove /dev/vda2 /dev/vdb1
PV metadata is wiped from partition or disk
Partition is free for reallocation or reformatting
To display information about physical volumes, use pvdisplay
If no arguments are specified, pvdisplay lists information for all PVs
If device is specified as argument, only that PV is shown
[root@server1 ~]# pvdisplay /dev/vda2
--- Physical volume ---
<1> PV Name /dev/vda2
<2> VG Name vg-alpha
<3> PV Size 256.00 MiB / not usable 4.00 MiB
Allocatable yes
<4> PE Size 4.00 MiB
Total PE 63
<5> Free PE 26
Allocated PE 37
PV UUID JWzDpn-LG3e-n2oi-9Etd-VT2H-PMem-1ZXwP1To display information about volume groups, use vgdisplay
If no arguments are specified, vgdisplay shows all VGs
If VG name is specified, only that VG is shown
[root@server1 ~]# vgdisplay vg-alpha
--- Volume group ---
VG Name vg-alpha
<1> System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
<2> VG Size 1012.00 MiB
PE Size 4.00 MiB
<3> Total PE 253
Alloc PE / Size 175 / 700.00 MiB
<4> Free PE / Size 78 / 312.00 MiB
VG UUID 3snNw3-CF71-CcYG-Llk1-p6EY-rHEv-xfUSezTo display information about logical volumes, use lvdisplay
If no arguments are specified, lvdisplay shows all LVs
If LV name is specified, only that LV is shown
[root@server1 ~]# lvdisplay /dev/vg-alpha/hercules
--- Logical volume ---
<1> LV Path /dev/vg-alpha/hercules
LV Name hercules
<2> VG Name vg-alpha
LV UUID 5IyRea-W8Zw-xLHk-3h2a-IuVN-YaeZ-i3IRrN
LV Write Access read/write
LV Creation host, time server1.example.com 2014-02-19 00:6:48 -0500
LV Status available
# open 1
<3> LV Size 700 MiB
<4> Current LE 175
Segments 3
Allocation inherit
Read ahead sectors auto
- current set to 8192
Block device 252:Man pages: lvm(8), pvcreate(8), vgcreate(8), lvcreate(8), pvremove(8), vgremove(8), lvremove(8), pvdisplay(8), vgdisplay(8), lvdisplay(8), fdisk(8), gdisk(8), parted(8), partprobe(8), and mkfs(8)
Add physical volumes to volume group to increase its disk space
Called extending volume group
Can assign new physical extents to logical volumes
Can remove unused physical volumes from volume group
Called reducing volume group
Use pvmove to move data from extents on one PV to extents on other PVs in volume group
Add new disk to existing volume group
Move data from older or slower disk to new disk
Remove old disk from volume group
Can be done while logical volumes in volume group are in use
Prepare physical device:
Use fdisk, gdisk, or parted to create new partition for use with LVM
Set partition type:
Use Linux LVM on LVM partitions
Use 0x8e for MBR-style partitions
If necessary, use partprobe to register new partition with kernel
[root@server1 ~]# fdisk /dev/vdb
Use m for help, p to print partition table, n to create new partition, t to change partition type, w to write changes, and q to quit. |
Create physical volume:
Use pvcreate to label partition (or other physical device) for use with LVM as physical volume
Writes header directly to PV to store LVM configuration data
PV is divided into PEs of fixed size; for example, 4MiB blocks
To label multiple devices at same time, add space-delimited device names
[root@server1 ~]# pvcreate /dev/vdb2
Extend volume group:
Use vgextend to add new PV to volume group
Use VG name and PV device name as arguments:
[root@server1 ~]# vgextend vg-alpha /dev/vdb2
Verify new space is available:
Use vgdisplay to confirm additional PEs are available
Check Free PE/Size in output
It should be greater than zero
[root@server1 ~]# vgdisplay vg-alpha --- Volume group --- VG Name vg-alpha ... Free PE / Size 178 / 712.00 MiB ...
Move physical extents:
Use pvmove to relocate physical extents used on physical volume to other PVs in VG.
Requires enough free extents in VG and all extents must come from other PVs
Use PV device name from which PEs will be moved as argument
[root@server1 ~]# pvmove /dev/vdb2
Before using pvmove, back up data stored on all logical volumes in volume group. An unexpected power loss during the operation may leave the volume group in an inconsistent state. This could cause loss of data on logical volumes in the volume group. |
Reduce volume group:
Use vgreduce to remove PV from volume group
Use VG name and PV device name as arguments:
[root@server1 ~]# vgreduce vg-alpha /dev/vdb2
To permanently stop using device as PV, use pvremove
Can increase LV size without downtime
Can add free PEs in VG to LV to extend capacity
Use to extend file system it contains
Verify that volume group has space available:
Use vgdisplay to verify that sufficient PEs are available
[root@server1 ~]# vgdisplay vg-alpha --- Volume group --- VG Name vg-alpha ... Free PE / Size 178 / 712.00 MiB ...
Check Free PE/Size for value equal to or greater than space required
If space is insufficient, extend volume group
Extend logical volume:
Use lvextend to extend logical volume size
Add LV device name as last argument
[root@server1 ~]# lvextend -L +300M /dev/vg-alpha/hercules
There are multiple ways to specify new size:
Use -l to specify physical extent values
Use -L to specify size in bytes, mebibytes, and gibibytes
Examples:
lvextend -l 128: Resize logical volume to exactly 128 extents
lvextend -l +128: Add 128 extents to current size of logical
volume
lvextend -L 128M: Resize logical volume to exactly 128MiB
lvextend -L +128M: Add 128MiB to current size of logical volume
lvextend -l +50%FREE: Add 50 percent of current VG free space to LV
Extend file system:
Use xfs_growfs /mountpoint to expand file system to occupy extended LV
Requires file system be mounted while being run
Can continue to use file system while resizing
[root@server1 ~]# xfs_growfs /mnt/hercules
Can also use lvextend -r to resize file system after LV is extended
Uses fsadm
Works with several different file systems
Verify new size of mounted file system
df -h /mountpoint
Verify volume group has space available:
Use vgdisplay vgname to verify enough PEs are available
Extend logical volume:
Use lvextend -l +extents/dev/vgname/lvname to extend logical volume
/dev/vgname/lvname by +extents.
Extend file system
Use resize2fs /dev/vgname/lvname to expand file system to occupy extended LV
Can mount file system use it while extending
To see progress of resize operation, use -p
[root@server1 ~]# resize2fs /dev/vg-alpha/hercules
Man pages: lvm(8), pvcreate(8), pvmove(8), vgdisplay(8), vgextend(8), vgreduce(8), vgdisplay(8), vgextend(8), vgreduce(8), lvextend(8), fdisk(8), gdisk(8), parted(8), partprobe(8), xfs_growfs(8), and resize2fs(8)
Managing Logical Volumes
Extending Logical Volumes
Nice job!
Click the button below to complete this module of the course:
Click the button below to continue to the course homepage:
Please continue with the next item in the course.